package com.itheima.knowledge.manager.service;

import com.itheima.knowledge.manager.domain.Catalog;
import com.itheima.knowledge.manager.domain.Knowledge;

import java.util.ArrayList;

import static com.itheima.knowledge.manager.entry.KnowledgeEntry.kd;

public class KnowledgeService {
    public boolean existsCatalog(String id) {
        ArrayList<Catalog> array = kd.findCatalog();
        for (int i = 0; i < array.size(); i++) {
            Catalog log = array.get(i);
            if (log != null && log.getLogid().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public void deleteCatalog(String id) {
        kd.deleteCatalog(id);
    }

    public void updataCatalog(String id, String know) {
        ArrayList<Catalog> array = kd.findCatalog();
        String oldKnow = null;
        for (int i = 0; i < array.size(); i++) {
            Catalog log = array.get(i);
            if (log != null && log.getLogid().equals(id)) {
                oldKnow = log.getLogknow();
            }
        }
        kd.updataCatalog(id, know,oldKnow);
    }

    public void addCatalog(Catalog catalog) {
        kd.addCatalog(catalog);
    }


    public ArrayList<Knowledge> findAllLogid(String know) {
        return kd.findAllLogid(know);
    }


    public boolean existsKnid(String id, String know) {
        ArrayList<Knowledge> array = kd.findAllLogid(know);
        for (int i = 0; i < array.size(); i++) {
            Knowledge knowledge = array.get(i);
            if (knowledge != null && knowledge.getKnid().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public void addKnowledge(Knowledge knd, String know) {
        kd.addKnowledge(knd, know);
    }

    public void deleteKnowledge(String id, String know) {
        if (existsKnid(id, know)) {
            kd.deleteKnowledge(id, know);
            System.out.println("删除成功!");
        } else {
            System.out.println("输入的编号有误!");
        }
    }


    public void updataKnowledge(String id, Knowledge knowledge,String know,int cho) {
        kd.updataKnowledge(id,knowledge,know,cho);

    }
}
